/*
 * Copyright (C) 2025 Thomas Akehurst
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.github.tomakehurst.wiremock.db.entity;

import com.github.tomakehurst.wiremock.common.Json;
import com.github.tomakehurst.wiremock.stubbing.StubMapping;
import java.time.LocalDateTime;

public class StubMappingEntity {
  private String id; // 对应 UUID
  private String content; // JSON 内容
  private Boolean isPersistent;
  private String sanitizedUrl;
  private String originalFilename;
  private LocalDateTime createdAt;
  private LocalDateTime updatedAt;

  // 手动生成 Getter 和 Setter
  public String getId() {
    return id;
  }

  public void setId(String id) {
    this.id = id;
  }

  public String getContent() {
    return content;
  }

  public void setContent(String content) {
    this.content = content;
  }

  public Boolean getIsPersistent() {
    return isPersistent;
  }

  public void setIsPersistent(Boolean persistent) {
    isPersistent = persistent;
  }

  public String getSanitizedUrl() {
    return sanitizedUrl;
  }

  public void setSanitizedUrl(String sanitizedUrl) {
    this.sanitizedUrl = sanitizedUrl;
  }

  public String getOriginalFilename() {
    return originalFilename;
  }

  public void setOriginalFilename(String originalFilename) {
    this.originalFilename = originalFilename;
  }

  public LocalDateTime getCreatedAt() {
    return createdAt;
  }

  public void setCreatedAt(LocalDateTime createdAt) {
    this.createdAt = createdAt;
  }

  public LocalDateTime getUpdatedAt() {
    return updatedAt;
  }

  public void setUpdatedAt(LocalDateTime updatedAt) {
    this.updatedAt = updatedAt;
  }

  /** 转换为 WireMock 的 StubMapping */
  public StubMapping toStubMapping() {
    try {
      // 使用 WireMock 内部的 JSON 工具反序列化
      StubMapping mapping = Json.read(content, StubMapping.class);
      mapping.setDirty(false); // 标记为“未修改”
      return mapping;
    } catch (Exception e) {
      throw new RuntimeException("Failed to deserialize StubMapping from JSON", e);
    }
  }
}
